package com.chwn.curiousdiary.ui.story.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.bean.StoryBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.CustomEvent;
import com.chwn.curiousdiary.service.AudioService;
import com.chwn.curiousdiary.ui.BaseShareActivity;
import com.chwn.curiousdiary.ui.play.activity.StoryPlayActivity;
import com.chwn.curiousdiary.ui.story.fragment.StoryListFragment;
import com.chwn.curiousdiary.utils.ImgLoadUtil;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Myshikin on 2017/4/11.
 */

public class StoryListActivity extends BaseShareActivity {


    @BindView(R.id.tv_toolbar_title)
    TextView  mStoryListTV;
    @BindView(R.id.toolbar)
    Toolbar   mToolBar;
    @BindView(R.id.top_view)
    View      mTopView;
    @BindView(R.id.ib_back)
    View      mBack;
    @BindView(R.id.iv_background)
    ImageView mBGImg;

    @BindView(R.id.iv_story_img)
    ImageView mStoryImg;
    @BindView(R.id.tv_story_name)
    TextView  mStoryName;
    @BindView(R.id.tv_story_desc)
    TextView  mStoryDesc;
    @BindView(R.id.card_view)
    CardView  mCardView;

    private String storyListID;
    private String storyListTitle;

    private String[] storyID   = new String[]{"dc4c8cf1075c461aac0322db7afb8042", "c3aa6b542c564a779c9f2bce235f893a", "4c86caf64c38426c9f45fcb74b592f1f"
            , "def98e39eeea4cc38bbcae03ea4f1160", "0ec9cab1cb6442e98689a7d6c4265f29", "f4aefe3d45ab446eaab2807d241e7d8c", "-1", "-2"};
    private String[] storyType = new String[]{"蜗牛小战警", "童话城堡", "奇幻传说", "生活奥秘", "国学经典", "萌趣生物", "最新故事", "个性推荐"};

    private int[] bgImgs = new int[]{R.drawable.bg_adventure, R.drawable.bg_build, R.drawable.bg_fancy, R.drawable.bg_life
            , R.drawable.bg_china_book, R.drawable.bg_animal, R.drawable.bg_newest};
    private int position;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_story_list;
    }

    @Override
    protected void onStart() {
        super.onStart();

//        EventBus.getDefault().post(new CustomEvent(CustomEvent.GET_PLAY_STORY));

        // 暂时注释
        // initPlayStory();
    }

    /**
     * 初始化正在播放的故事展示
     */
    private void initPlayStory() {
        if (data != null && position == 0) {
            mCardView.setVisibility(View.VISIBLE);
            ImgLoadUtil.displayCircle(mStoryImg, data.getStoryImage());
            mStoryName.setText(data.getStoryTitle());
            mStoryDesc.setText(data.getStoryDetailTitle());
        } else {
            mCardView.setVisibility(View.GONE);
        }
    }

    private StoryBean data;

    /**
     * @param event
     */
  /*  public void onEventMainThread(CustomEvent event) {
        switch (event.getState()) {
            case CustomEvent.SEND_PLAY_STORY:
                data = (StoryBean) event.getMessage();
                break;
        }
    }*/

    @Override
    protected View getLocationView() {
        return mBGImg;
    }

    @Override
    protected void initData() {
        super.initData();
        position = getIntent().getIntExtra(Constant.JUMP_TYPE, 0);
        storyListTitle = storyType[position];
        storyListID = storyID[position];
        // 去掉背景展示
        // mBGImg.setImageResource(bgImgs[position]);
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

        ImmersionBar.with(this)
                .statusBarView(R.id.top_view)
                .statusBarDarkFont(true, 0.2f)
                .titleBar(mToolBar, true)
                .init();

        mStoryListTV.setText(storyListTitle);


        final StoryListFragment storyListFragment = new StoryListFragment();
        Bundle args = new Bundle();
        args.putCharSequence(Constant.STORY_LIST, storyListID);
        args.putInt(Constant.JUMP_TYPE, position);
        storyListFragment.setArguments(args);
        storyListFragment.setLoadDataNow(true);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fl_content, storyListFragment)
                .commit();

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StoryListActivity.this, StoryPlayActivity.class);
                intent.putExtra(Constant.STORY_SERVICE_TYPE, AudioService.FROM_MAIN);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
